package com.tanhua.tanhua.service;

import com.tanhua.tanhua.dao.DashboardDao;
import com.tanhua.tanhua.pojo.Dashboard;
import com.tanhua.tanhua.service.cs.Checktoken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Service
public class DashboardService {

    @Value("${jwt.secret}")
    private String secret;

    @Autowired
    DashboardDao dashboardDao;
    private static SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
    private static Date date = new Date();
    private long days = (1000 * 60) * 60 * 24;


    public Map finddashboarddata(String token) {


        Boolean aBoolean = Checktoken.checktoken(token, secret);
        if (!aBoolean) {

            return null;
        }


        Map<String, Integer> map = new HashMap<String, Integer>();
        /**
         * 查询累计用户数量
         */
        Integer all = dashboardDao.findAll();
        map.put("cumulativeUsers", all);
        /**
         * 查询30天之内活跃用户
         */
        Integer integer = findin30dayactive();
        map.put("activePassMonth", integer);
        /**
         * 查询7天之内活跃用户
         */
        Integer integer1 = find7BeforeActive();
        map.put("activePassWeek", integer);
        /**
         * 查询今日新增用户
         */
        Integer integer2 = findtodaynewadd();
        map.put("newUsersToday", integer);
        /**
         * 查询今日新增用户涨跌率
         */
        Integer integer3 = findtodayrate();

        map.put("newUsersTodayRate", integer3);
        /**
         * 查询今日登录次数
         */
        Integer findtodaylogin = findtodaylogin();
        map.put("loginTimesToday",findtodaylogin);
        /**
         * 查询今日登录次数涨跌率
         */
        Integer findtodayloginrate= findtodayloginrate();
        map.put("loginTimesTodayRate", findtodayloginrate);
        /**
         * 查询今日活跃用户
         */
        Integer i=  findtodayactive();
        map.put("activeUsersToday", i);
        /**
         * 查询今日活跃用户涨跌率
         */
        Integer findtodayactiverate=findtodayactiverate();
        map.put("activeUsersTodayRate", findtodayactiverate);





        map.put("useTimePassWeek", integer);
        /**
         * 查询昨天活跃用户
         */
       Integer findyesterdayactive= findyesterdayactive();
        map.put("activeUsersYesterday", findyesterdayactive);
        /**
         * 查询昨天活跃用户涨跌率
         */
      Integer integer4=  findyesterdayactiverate();
        map.put("activeUsersYesterdayRate", integer4);


        return map;
    }


    private Integer findyesterdayactiverate() {

        Integer yesterdayactive = findyesterdayactive();

        long time = date.getTime();
        long l = time - (2 * days);
        Date date = new Date(l);
        String format = DashboardService.format.format(date);
        Integer qtactive = dashboardDao.findactive(format);


        String result = null;

        if (yesterdayactive > qtactive) {
            long i = yesterdayactive - qtactive;
            NumberFormat numberFormat = NumberFormat.getInstance();
            numberFormat.setMaximumFractionDigits(0);
            result = numberFormat.format((float) i / (float) qtactive * 100);
        } else if (qtactive > yesterdayactive) {
            long i = qtactive - yesterdayactive;
            NumberFormat numberFormat = NumberFormat.getInstance();
            numberFormat.setMaximumFractionDigits(0);
            result ="-"+ numberFormat.format((float) i / (float) qtactive * 100);
        } else {
            result = "0";
        }
        return Integer.parseInt(result);

    }

    private Integer findyesterdayactive() {
        long time = date.getTime();
        long l = time - days;
        Date date = new Date(l);

        String format = DashboardService.format.format(date);
        Integer findactive = dashboardDao.findactive(format);

        return findactive;

    }

    private Integer findtodayactiverate() {
        Integer findtodayactive = findtodayactive();

        long time = date.getTime();
        long l = time - days;
        Date date1 = new Date(l);
        String yesterday = DashboardService.format.format(date1);
        //查询昨天用户登陆次数
        Integer yesterdayactice = dashboardDao.findactive(yesterday);
        String result = null;

        if (findtodayactive > yesterdayactice) {
            long i = findtodayactive - yesterdayactice;
            NumberFormat numberFormat = NumberFormat.getInstance();
            numberFormat.setMaximumFractionDigits(0);
            result = numberFormat.format((float) i / (float) yesterdayactice * 100);
        } else if (yesterdayactice > findtodayactive) {
            long i = yesterdayactice - findtodayactive;
            NumberFormat numberFormat = NumberFormat.getInstance();
            numberFormat.setMaximumFractionDigits(0);
            result ="-"+ numberFormat.format((float) i / (float) yesterdayactice * 100);
        } else {
            result = "0";
        }
        return Integer.parseInt(result);

    }

    private Integer findtodayactive() {
        String today = DashboardService.format.format(date);
        Integer findtodayactive = dashboardDao.findactive(today);
        return findtodayactive;

    }

    private Integer findtodayloginrate() {

        //今日用户登录次数
        Integer findtodaylogin = findtodaylogin();
        long time = date.getTime();
        long l = time - days;
        Date date1 = new Date(l);
        String yesterday = DashboardService.format.format(date1);
        //查询昨天用户登陆次数
        Integer yesterdaylogin = dashboardDao.findlogin(yesterday);
        String result = null;

        if (findtodaylogin > yesterdaylogin) {
            long i = findtodaylogin - yesterdaylogin;
            NumberFormat numberFormat = NumberFormat.getInstance();
            numberFormat.setMaximumFractionDigits(0);
            result = numberFormat.format((float) i / (float) yesterdaylogin * 100);
        } else if (yesterdaylogin > findtodaylogin) {
            long i = yesterdaylogin - findtodaylogin;
            NumberFormat numberFormat = NumberFormat.getInstance();
            numberFormat.setMaximumFractionDigits(0);
            result ="-"+ numberFormat.format((float) i / (float) yesterdaylogin * 100);
        } else {
            result = "0";
        }
        return Integer.parseInt(result);
    }

    private Integer findtodaylogin() {
        String format = DashboardService.format.format(date);
        Integer login = dashboardDao.findlogin(format);
        return login;
    }

    private Integer findtodayrate() {
        //今天的新增用户
        String today = DashboardService.format.format(date);
        Integer todaynewaddnum = dashboardDao.findtodaynewadd(today);

        long time = date.getTime();
        long l = time - days;
        Date date1 = new Date(l);
        String yesterday = DashboardService.format.format(date1);
        Integer yesterdaynum = dashboardDao.findyesterdaynewadd(yesterday);

        String result = null;
        if (todaynewaddnum > yesterdaynum) {
            long i = todaynewaddnum - yesterdaynum;
            NumberFormat numberFormat = NumberFormat.getInstance();
            numberFormat.setMaximumFractionDigits(0);
            result = numberFormat.format((float) i / (float) yesterdaynum * 100);
        } else if (yesterdaynum > todaynewaddnum) {
            long i = yesterdaynum - todaynewaddnum;
            NumberFormat numberFormat = NumberFormat.getInstance();
            numberFormat.setMaximumFractionDigits(0);
            result = "-"+numberFormat.format((float) i / (float) yesterdaynum * 100);
        } else {
            result = "0";
        }
        return Integer.parseInt(result);
    }

    private Integer findtodaynewadd() {

        String today = DashboardService.format.format(date);
        Integer findtodaynewadd = dashboardDao.findtodaynewadd(today);
        return findtodaynewadd;

    }

    private Integer find7BeforeActive() {

        long time = date.getTime();
        long l = time - (days * 7);

        String nowdateString = DashboardService.format.format(date);
        String before7dateString = DashboardService.format.format(l);

        Integer integer = dashboardDao.find7BeforeActive(before7dateString, nowdateString);
        return integer;
    }


    private Integer findin30dayactive() {

        long time = date.getTime();
        long months = time - (days * 30);
        Date monthsdate = new Date(months);

        String monthsString = format.format(monthsdate);
        String nowString = format.format(date);
        Integer integer = dashboardDao.find30BeforeActive(monthsString, nowString);
        return integer;
    }


}
